package com.example.mylibrary.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.mylibrary.di.component.BaseComponent;
import com.trello.rxlifecycle2.components.support.RxFragment;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;

/**
 * 基础Fragment
 *
 * @author mark.liu
 */
public abstract class BaseFragment<P extends IPresenter> extends RxFragment {
    protected final String TAG = this.getClass().getSimpleName();
    protected Activity mActivity;
    protected final CompositeDisposable mDisposable = new CompositeDisposable();
    private Unbinder mUnbinder;
    @Inject protected P mPresenter;
    /**
     * 视图是否已经初初始化
     */
    protected boolean isInit = false;
    protected boolean isLoad = false;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutRes(), container, false);
        // 绑定到butterknife
        mUnbinder = ButterKnife.bind(this, view);
        isInit = true;
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = getActivity();
        // 如果要使用eventbus请将此方法返回true
        if (useEventBus()) {
            // 注册到事件主线
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
        }
//        setupFragmentComponent(BaseApplication.getBaseComponent());
        isCanLoadData();
//        initData();
    }

    /**
     * 视图是否已经对用户可见，系统的方法
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isCanLoadData();
    }

    /**
     * 是否可以加载数据
     * 可以加载数据的条件：
     * 1.视图已经初始化
     * 2.视图对用户可见
     */
    private void isCanLoadData() {
        if (!isInit) {
            return;
        }
        if (getUserVisibleHint()) {
            initData();
            isLoad = true;
        } else {
            if (isLoad) {
                stopLoad();
            }
        }
    }
    /**
     * 提供AppComponent(提供所有的单例对象)给子类，进行Component依赖
     *
     * @param baseComponent 父组件
     */
    protected abstract void setupFragmentComponent(BaseComponent baseComponent);

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != Unbinder.EMPTY) {
            mUnbinder.unbind();
        }
        isInit = false;
        isLoad = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mDisposable && !mDisposable.isDisposed()) {
            mDisposable.clear();
        }
        if (mPresenter != null) {
            mPresenter.onDestroy();// 释放资源
        }
        // 如果要使用eventbus请将此方法返回true
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }
        this.mPresenter = null;
        this.mActivity = null;
        this.mUnbinder = null;
    }

    /**
     * 是否使用eventBus,默认为使用(false)，
     */
    protected boolean useEventBus() {
        return false;
    }

    /**
     * 加载xml布局文件
     */
    @LayoutRes
    protected abstract int getLayoutRes();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 当视图已经对用户不可见并且加载过数据，如果需要在切换到其他页面时停止加载数据，可以调用此方法
     */
    protected void stopLoad() {
    }

    public void launchActivity(@NonNull Intent intent) {
        ActivityUtils.startActivity(mActivity, intent);
    }

    public void launchActivityForResult(@NonNull Intent intent,int requestCode) {
        startActivityForResult(intent,requestCode);
    }

    public void killMyself() {
        ActivityUtils.finishActivity(mActivity);
    }

    public void showToast(String msg) {
        ToastUtils.showLong(msg);
    }

    public void showToast(int resId) {
        ToastUtils.showShort(resId);
    }
}
